<?php

/* 访问控制 */
defined('IN_ECTOUCH') or die('Deny Access');

class IntegralModel extends BaseModel {

    function check_time(){
        $this->user_id = $_SESSION['user_id'];
		$time_num = 0;
		$fs_arr = $this->model->table('account_log')->where( "user_id= ".$this->user_id." and fs_type = 3" )->order("change_time desc")->limit(30)->select();
		if(!empty($fs_arr)){
			$new_num = $this->model->table('account_log')->field('count(*)')->where( "user_id= ".$this->user_id." and fs_type = 3 and FROM_UNIXTIME(change_time,'%Y-%m-%d') ='".date('Y-m-d')."'" )->getOne();
			$Days = $this->count_days(time(), ($new_num>0&&count($fs_arr)>1 ? $fs_arr[1]['change_time'] : $fs_arr[0]['change_time']));
			if($Days == 1 || date('Y-m-d', $fs_arr[0]['change_time']) == date('Y-m-d')) $time_num = 1;

			foreach($fs_arr as $_k=>$_v){
				$time_new = $_v['change_time'];
				$time_old = $this->model->table('account_log')->field('change_time')->where( "user_id= ".$this->user_id." and fs_type = 3 and log_id < '$_v[log_id]'" )->order("change_time desc")->limit(1)->getOne();

				$Days = $this->count_days($time_new, $time_old);
				if($Days == 1 && ($fs_time == $time_new || !$_k)){
					$fs_time = $time_old;
					$time_num += 1;
				}
			}
		}
		return $time_num;
	}
	function count_days($a,$b){
		$a_dt = getdate($a);
		$b_dt = getdate($b);
		$a_new = mktime(12, 0, 0, $a_dt['mon'], $a_dt['mday'], $a_dt['year']);
		$b_new = mktime(12, 0, 0, $b_dt['mon'], $b_dt['mday'], $b_dt['year']);
		return round(abs($a_new-$b_new)/86400);
	}

}
